/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System.ComponentModel;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "FilterBy = {DebuggerDisplayString}" )]
  public abstract class FilterExpressionBase 
  {
    #region Constructor

    internal FilterExpressionBase()
    {
    }

    #endregion

    #region IsFrozen Property

    internal bool IsFrozen
    {
      get
      {
        return m_isFrozen;
      }
    }

    private bool m_isFrozen; //false

    #endregion

    #region DebuggerDisplayString Internal Property

    internal string DebuggerDisplayString
    {
      get
      {
        FilterExpressionFormatter formatter = new DebuggerDisplayFilterExpressionFormatter( this );

        return formatter.ToString();
      }
    }

    #endregion

    [DebuggerStepThrough]
    public static bool operator ==(
      FilterExpressionBase left,
      FilterExpressionBase right )
    {
      return object.Equals( left, right );
    }

    [DebuggerStepThrough]
    public static bool operator !=(
      FilterExpressionBase left,
      FilterExpressionBase right )
    {
      return !object.Equals( left, right );
    }

    public override int GetHashCode()
    {
#if WINDOWS_PHONE
      this.Freeze();
#endif

      return base.GetHashCode();
    }

    public override bool Equals( object obj )
    {
#if WINDOWS_PHONE
      this.Freeze();
#endif

      return base.Equals( obj );
    }

    internal abstract FilterExpressionBase Clone();

    internal abstract void Accept( FilterExpressionVisitor visitor );

    internal void Freeze()
    {
      if( m_isFrozen )
        return;

      //Any errors while freezing will throw out of this method up to the action that caused the freeze.
      this.FreezeOverride();

      m_isFrozen = true;
    }

    internal virtual void FreezeOverride()
    {
    }
  }
}
